Skip to content

Adding functionality to config preferred authschemeProvider #6083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

RanVaknin
Copy link
Contributor

@RanVaknin RanVaknin commented May 2, 2025

Motivation and Context

Previously, when multiple auth schemes were available for an operation, the SDK would choose the first one defined in the service model. This PR implements the auth scheme preference configuration that allows users to specify their preferred authentication schemes in order of preference when multiple auth schemes are supported.

Example usage:

// Via client configuration in code
MyServiceClient client = MyServiceClient.builder()
    .authSchemeProvider(MyServiceAuthSchemeProvider.builder()
        .withPreferredAuthSchemes(Arrays.asList("sigv4", "sigv4a"))
        .build())
    .build();

// Via JVM properties:
// in code
System.setProperty("aws.authSchemePreference", "sigv4,sigv4a");
// or as a cmd line argument
java -Daws.authSchemePreference=sigv4,sigv4a -jar your-application.jar

// Via Environment variable:
export AWS_AUTH_SCHEME_PREFERENCE=sigv4,sigv4a

// Via AWS config file (~/.aws/config):
[default]
auth_scheme_preference = sigv4,sigv4a

Modifications

  • [Modified] client builders to read and apply auth scheme preferences

  • [Modified] the auth scheme resolution logic to respect user preferences while maintaining backward compatibility

  • [Added] AuthSchemePreferenceProvider class to resolve auth scheme preferences from various sources:

    • Client configuration
    • JVM system properties (aws.authSchemePreference)
    • Environment variables (AWS_AUTH_SCHEME_PREFERENCE)
    • AWS config file (auth_scheme_preference)
  • [Added] code generation support through PreferredAuthSchemeProviderSpec to generate service-specific auth scheme providers

Testing

  • AuthSchemePreferenceProviderTest verifies proper parsing of auth scheme preferences from different formats (spaces, tabs, etc.)

  • PreferredAuthSchemeProviderTest to test the reordering of auth schemes according to preferences
    comprehensive test cases for preference resolution from multiple sources, verifying proper precedence:

  • Stubbed functional test with mock services to verify the selected auth scheme matches the expected preference in actual requests

@RanVaknin RanVaknin force-pushed the rvaknin/auth-schem-preference-config branch from 362e5f3 to f18fcc2 Compare May 5, 2025 02:22
@alextwoods alextwoods mentioned this pull request May 23, 2025
12 tasks
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
69.3% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@RanVaknin RanVaknin marked this pull request as ready for review May 26, 2025 04:51
@RanVaknin RanVaknin requested a review from a team as a code owner May 26, 2025 04:51
@@ -1,27 +1,15 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we are removing copyright?

return new QueryAuthSchemeProviderBuilder();
}

interface Builder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import software.amazon.awssdk.utils.Lazy;

@SdkProtectedApi
public class AuthSchemePreferenceProvider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final

import software.amazon.awssdk.utils.Lazy;

@SdkProtectedApi
public class AuthSchemePreferenceProvider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +123 to +127
try {
client.multiAuthWithOnlySigv4aAndSigv4(MultiAuthWithOnlySigv4AAndSigv4Request.builder().build());
} catch (AutSchemeCapturingInterceptor.CaptureException e) {
// expected
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use assertThatThrownBy

Comment on lines +110 to +119
private static List<String> parseAuthSchemeList(String unformattedList) {
if (unformattedList == null) {
return Collections.emptyList();
}

unformattedList = unformattedList.replaceAll("\\s+", "");
String[] splitByTabs = unformattedList.split("\t");
String finalFormat = String.join("", splitByTabs);
return Arrays.asList(finalFormat.split(","));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work?"\\s+" should handle tab.

   private static List<String> parseAuthSchemeList(String unformattedList) {
       if (unformattedList == null) {
           return Collections.emptyList();
       }

       return Arrays.asList(unformattedList.replaceAll("\\s+", "").split(","));
   }

import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.utils.CollectionUtils;

public class PreferredAuthSchemeProviderSpec implements ClassSpec {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add test case for this class?

Comment on lines +92 to +94
b.addStatement("String candidateSchemeName = candidate.schemeId().contains(\"#\") ? " +
"candidate.schemeId().split(\"#\")[1] : candidate.schemeId()");
b.addStatement("return candidateSchemeName.equals(preferredSchemeId)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why do we need to handle# here?

}

@ParameterizedTest
@MethodSource("schemeParsingCases")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we add a test name for each parameter?

return new QueryAuthSchemeProviderBuilder();
}

interface Builder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing javadoc and NotThreadSafe annotation`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants